/*
 * Copyright (c) 2009.
 * nick@tmro.net
 */
package net.tmro.loko.domain.category;

import net.tmro.loko.domain.PersistentEntity;
import net.tmro.loko.domain.store.Store;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Date;
import java.util.List;

/**
 * @author nick
 */
@XmlRootElement
@Entity
@Table(name = "CATEGORY")
public class Category extends PersistentEntity {

    @Column(name = "NAME")
    private String name = "default name";

    @Column(name = "VERSION")
    private int version = 1;

    /**
     * Date when the category was created
     */
    @Column(name = "DATE_CREATED")
    private Date dateCreated;

    /**
     * Date when this category was last modified
     */
    @Column(name = "DATE_MODIFIED")
    private Date dateModified;

    @OneToMany(mappedBy = "category", cascade = CascadeType.ALL)
    private List<Store> stores;

    /**
     * Required for JAXB
     */
    public Category() {
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    public Date getDateModified() {
        return dateModified;
    }

    public void setDateModified(Date dateModified) {
        this.dateModified = dateModified;
    }

    public List<Store> getStores() {
        return stores;
    }

    public void setStores(List<Store> stores) {
        this.stores = stores;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Category that = (Category) o;

        if (version != that.version) return false;
        if (dateCreated != null ? !dateCreated.equals(that.dateCreated) : that.dateCreated != null) return false;
        if (dateModified != null ? !dateModified.equals(that.dateModified) : that.dateModified != null) return false;
        if (getId() != null ? !getId().equals(that.getId()) : that.getId() != null) return false;
        if (!name.equals(that.name)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = getId() != null ? getId().hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (int) (version ^ (version >>> 32));
        result = 31 * result + (dateCreated != null ? dateCreated.hashCode() : 0);
        result = 31 * result + (dateModified != null ? dateModified.hashCode() : 0);
        return result;
    }
}